﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Timers;

namespace Ken_WPF_UI.View
{
    /// <summary>
    /// ProgressBarWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ProgressBarWindow : Window
    {
        protected Timer timer = new Timer();


        public ProgressBarWindow()
        {
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.progressBar.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF63EEEE"));//浅绿色
            //定时任务
            timer.Elapsed += new System.Timers.ElapsedEventHandler(TimerElapsed);
            timer.Interval = 1000;
            timer.Start();
        }

        private void TimerElapsed(object sender, ElapsedEventArgs e)
        {

            this.progressBar.Dispatcher.Invoke(new Action(delegate
            {
                this.progressBar.Value += 10;
                this.label1.Content = this.progressBar.Value + "%";
                if (this.progressBar.Value >= this.progressBar.Maximum*0.5 && this.progressBar.Value <= this.progressBar.Maximum * 0.8)
                {
                    this.progressBar.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF31D3D3"));//深绿色
                    //new SolidColorBrush(ColorTranslator.FromHtml("#FF0000"));
                    this.progressBar.IsIndeterminate = false;//加载状态取消
                }

                if (this.progressBar.Value >= this.progressBar.Maximum)
                {
                    this.progressBar.Foreground = new SolidColorBrush(Color.FromRgb(0, 128, 128));//Teal色
                    timer.Stop();
                    this.progressBar.IsIndeterminate = false;//加载状态取消
                    MessageBox.Show("进度已满");
                }
            }));
           
        }

        /// <summary>
        /// 如果默认有值的时候  在InitializeComponent还没有执行完全 就进入这个方法 会出现空指针
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProgressBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (this.label1 != null)
            {
                var value = this.progressBar.Value;
                this.label1.Content = value + "%";
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.progressBar.Value += 10;
        }
    }
}
